<template>
  <div class="article-container">
      <!-- 文章列表组件 -- {{channel.name}} -- {{channel.id}} -->
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh" :loading-text="loadingText" :success-duration="3000">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
        <article-item v-for="(item,index) in list" :key="index" :article="item"></article-item>
        <!-- 这块东西我们打算用组件来写 公共组件来实现 -->
          <!-- <van-cell v-for="(item,index) in list" :key="index" :title="item.title" /> -->
        </van-list>
      </van-pull-refresh>
  </div>
</template>
<script>
import { getArticleList } from '@/api/article'
import ArticleItem from '@/components/article-item'
export default {
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      requred: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      refreshing: false,
      loadingText: '加载中....'
    }
  },
  methods: {
    async onLoad () {
      try {
        const { data: res } = await getArticleList({
          channel_id: this.channel.id,
          timestamp: this.timestamp,
          with_top: 1
        })
        console.log(res)
        this.list.push(...res.data.results)
        this.timestamp = res.data.pre_timestamp
        this.loading = false // 加载状态结束
        if (res.data.results.length === 0) {
          this.finished = true// 数据加载结束
        }
      } catch (err) {
        this.$toast.fail('请求列表数据失败')
        this.error = true
      }
    },
    async onRefresh () {
      try {
        const { data: res } = await getArticleList({
          channel_id: this.channel.id,
          timestamp: this.timestamp,
          with_top: 1
        })
        console.log(res)
        this.list.unshift(...res.data.results)
        this.timestamp = res.data.pre_timestamp
        this.refreshing = false
        this.loadingText = `加载了${res.data.results.length}数据`
      } catch (err) {
        this.$toast.fail('请求列表数据失败')
        this.error = true
      }
    }
  }
}
</script>
<style scoped lang="less">
.article-container{
  padding-bottom: 100px;
  height:79vh;
  // 超出了79vh的话，就会出现滚动条
  overflow-y: auto;
}
</style>
